package com.chaodev.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.dao.ActivityArticleDao;
import com.chaodev.base.dao.CommDao;
import com.chaodev.base.dto.ActivityArticleDto;
import com.chaodev.base.em.BizTypeEnum;
import com.chaodev.base.em.OperEnum;
import com.chaodev.base.model.*;
import com.chaodev.base.service.ActivityArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * vcbase
 *
 * @author 95634
 * @描述
 * @date 2023/5/19 15:56
 **/

@Service
public class ActivityArticleServiceImpl implements ActivityArticleService {

    @Autowired
    ActivityArticleDao articleDao;
    @Autowired
    CommDao commDao;

    @Override
    public ActivityArticle findDataById(String id) {
        return articleDao.findDataById(id);
    }

    @Override
    public void saveData(ActivityArticle article) throws DataAccessException {
        if(ObjectUtil.isNotNull(article.getId())&&article.getId()>0){
            articleDao.updateDataById(article);
        }else{
            articleDao.insertData(article);

            ApplyLc applyLc = new ApplyLc();
            applyLc.setBizType(BizTypeEnum.Em_Article.getValue());
            applyLc.setBizId(article.getId());
            applyLc.setReqState(1);
            applyLc.setLcId("lc_001");
            applyLc.setResultCode(OperEnum.Em_Add.getValue());
            applyLc.setResultDsc(OperEnum.Em_Add.getName());
            applyLc.setOpr(article.getcUser());
            applyLc.setOpTime(new Date());
            applyLc.setState(2);
            commDao.saveApply(applyLc);

            ApplyStateToNext stateToNext = new ApplyStateToNext();
            stateToNext.setBizType("1003");
            stateToNext.setReqState(1);
            stateToNext.setNowLcId("lc_001");
            stateToNext.setOp(OperEnum.Em_Add.getValue());
            ApplyStateToNext nextFlowInfo = commDao.getNextFlowInfo(stateToNext);

            Task task = new Task();
            task.setBizType(BizTypeEnum.Em_Article.getValue());
            task.setTitle(article.getTheme());
            task.setBizId(article.getId());
            task.setLcId(nextFlowInfo.getNextLcId());
            task.setLcName(nextFlowInfo.getNextLcName());
            task.setCUser(article.getcUser());
            task.setState(1);
            commDao.saveTask(task);

            applyLc = new ApplyLc();
            applyLc.setBizType(BizTypeEnum.Em_Article.getValue());
            applyLc.setBizId(article.getId());
            applyLc.setReqState(1);
            if(StrUtil.isNotEmpty(nextFlowInfo.getNextLcId())) {
                applyLc.setLcId(nextFlowInfo.getNextLcId());
            }
            applyLc.setState(1);
            commDao.saveApply(applyLc);
        }
    }

    @Override
    public void insertData(ActivityArticle article) throws DataAccessException {
        articleDao.insertData(article);
    }

    @Override
    public void updateDataById(ActivityArticle article) throws DataAccessException {
        articleDao.updateDataById(article);
    }

    @Override
    public void deleteDataById(String id) throws DataAccessException {
        articleDao.deleteDataById(id);
        Task task = new Task();
        task.setBizType(BizTypeEnum.Em_Article.getValue());
        task.setDelId(id);
        commDao.delTask(task);
    }

    @Override
    public List<ActivityArticle> queryDataList(ActivityArticle article) {
        return articleDao.queryDataList(article);
    }

    @Override
    public PageInfo<ActivityArticle> queryPageList(ActivityArticleDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getRows());
        ActivityArticle article = new ActivityArticle();
        BeanUtil.copyProperties(dto,article);
        List<ActivityArticle> tbActivityArticleList = articleDao.queryDataList(article);
        PageInfo<ActivityArticle> pageInfo = new PageInfo<ActivityArticle>(tbActivityArticleList, dto.getRows());
        return pageInfo;
    }

    @Override
    public void updateVisitNum(ActivityArticle tbAdvice) {
        ActivityArticle dataById = articleDao.findDataById(tbAdvice.getId() + "");
        Long visitNum = dataById.getVisitNum() == null ? 0 : dataById.getVisitNum();
        tbAdvice.setVisitNum(visitNum + 1);
        articleDao.updateVisitNum(tbAdvice);
    }

    @Override
    public PageInfo<ActivityArticle> queryPageAllList(ActivityArticleDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getRows());
        ActivityArticle article = new ActivityArticle();
        BeanUtil.copyProperties(dto,article);
        List<ActivityArticle> tbActivityArticleList = articleDao.queryAllDataList(article);
        PageInfo<ActivityArticle> pageInfo = new PageInfo<ActivityArticle>(tbActivityArticleList, dto.getRows());
        return pageInfo;
    }

    @Override
    public List<ActivityNotice> getMySignUpSchedule(String userId, Date thisDay) {
//        SimpleDateFormat DAY_FORMAT=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        return articleDao.getMySignUpSchedule(userId,DAY_FORMAT.format(thisDay));

        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if (thisDay != null){
            LocalDate localDate = thisDay.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDateTime beginTime = localDate.atStartOfDay();
            LocalDateTime endTime = localDate.atTime(23,59,59);
            return articleDao.getMySignUpSchedule(userId, beginTime.format(fmt), endTime.format(fmt));
        }else {
            LocalDateTime beginTime = null;
            LocalDateTime endTime = null;
            return articleDao.getMySignUpSchedule(userId,null,null);
        }

    }
}
